<?php
class UsuariosFinalesModel extends CustomModel
{

    public function getUsuariosFinales($filter, $pageNumber)
    {
        $usuariosAbm = new ABM('users');
        $subSelect = new SubSelect('(SELECT MAX(acl2.log_timestamp) FROM access_log AS acl2 where acl2.fkuser = users.iduser)');
        $joins = array(
            'profiles'      => array('profiles.fkuser' => 'users.iduser'),
            'pending_users' => array('pending_users.fkuser' => 'users.iduser'),
            'access_log'    => array(
                'type' => 'LEFT',
                'on'   => array(
                    'access_log.fkuser'        => 'users.iduser',
                    'access_log.access_type'   => ACCESS_LOG_TYPE_LOGIN,
                    'access_log.log_timestamp' => $subSelect
                )
            )
        );
        $userType   = filter('user_type', '=', USER_TYPE_NORMAL);
        $userStatus = filter('user_status', '=', STATUS_ONLINE);
        $filterReal = filter($userType, 'AND', $userStatus);
        // Reg date
        if ((!empty($filter['register_from_ts'])) && (!empty($filter['register_to_ts'])))
        {
            $registerFrom = filter('pending_users.creation_timestamp', '>=', $filter['register_from_ts']);
            $registerTo   = filter('pending_users.creation_timestamp', '<=', $filter['register_to_ts']);
            $register     = filter($registerFrom, 'AND', $registerTo);
            $filterReal   = filter($filterReal, 'AND', $register);
        }
        // Login date
        if ((!empty($filter['login_from_ts'])) && (!empty($filter['register_to_ts'])))
        {
            $loginFrom = filter('access_log.log_timestamp', '>=', $filter['login_from_ts']);
            $loginTo   = filter('access_log.log_timestamp', '<=', $filter['register_to_ts']);
            $login     = filter($loginFrom, 'AND', $loginTo);
            $filterReal = filter($filterReal, 'AND', $login);
        }
        // Email
        if(!empty($filter['email']))
        {
            $email      = filter('users.email', '=', $filter['email']);
            $filterReal = filter($filterReal, 'AND', $email);
        }
        // Username
        if(!empty($filter['username']))
        {
            $username   = filter('users.username', '=', $filter['username']);
            $filterReal = filter($filterReal, 'AND', $username);
        }
        // Fname
        if(!empty($filter['fname']))
        {
            $fname      = filter('profiles.fname', '=', $filter['fname']);
            $filterReal = filter($filterReal, 'AND', $fname);
        }
        // Lname
        if(!empty($filter['lname']))
        {
            $lname      = filter('profiles.lname', '=', $filter['lname']);
            $filterReal = filter($filterReal, 'AND', $lname);
        }
        // Per page
        if ($filter['per_page'] == 'max')
        {
            $filter['per_page'] = 999999999;
        }
        // Get
        $usuarios = $usuariosAbm->getPaginatedWithJoin($pageNumber, $filterReal, $joins, $filter['per_page'], 'iduser ASC', true);
        return $usuarios;
    }

    public function borrarUsuariosFinales($users)
    {
        $list = array();
        foreach($users as $user)
        {
            $list[]=$user;
        }
        $usuariosAbm = new ABM('users');
        if ($usuariosAbm->update(array('iduser' => $list, 'user_type' => USER_TYPE_NORMAL), array('user_status' => STATUS_OFFLINE)))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
}
?>